package musicalgo.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileIO
{

    public static List<String> readLines(String path)
    {
        Scanner scanner = null;
        File f = new File(path);
        System.out.println("loading file: " + f.getAbsolutePath());
        try
        {
            scanner = new Scanner(f);
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        List<String> lines = new ArrayList<String>();

        try
        {
            while (scanner.hasNextLine())
            {
                lines.add(scanner.nextLine());
            }
            return lines;
        } finally
        {
            scanner.close();
        }
    }


    public static String[] stripCSV(String s)
    {
        return s.split(", *");
    }

    public static String[] spaceSplit(String s)
    {
        return s.split(" +");
    }

    public static void writeLines(List<String> lines, String path)
    {

        String lineSeparator = System.getProperty("line.separator");
        File file = new File(path);
        System.out.println("Writing to file: " + file.getAbsolutePath());
        if (file.exists()) file.delete();
        FileWriter writer = null;
        try
        {
            writer = new FileWriter(file);
            for (String s : lines)
            {
                writer.write(s + lineSeparator);
            }
        } catch (IOException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally
        {
            if (writer != null)
            {
                try
                {
                    writer.close();
                } catch (IOException e) { }
            }
        }


    }

}
